package com.zat.mes.andrmes.dofun

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.hardware.Camera
import android.os.Vibrator
import android.text.TextUtils
import android.util.Log
import android.view.View
import cn.bingoogolapple.qrcode.core.BarcodeType
import cn.bingoogolapple.qrcode.core.QRCodeView
import com.zat.mes.andrmes.R
import com.zat.mes.andrmes.base.BaseActivity
import com.zat.mes.andrmes.global.CompanionType
import com.zat.mes.andrmes.utils.CameraUtil
import com.zat.mes.andrmes.utils.StatusBarUtils
import com.zat.mes.andrmes.utils.showToast
import kotlinx.android.synthetic.main.activity_qr_scan.*

/**
 * 二维码扫描
 */
class QrScanActivity : BaseActivity(), QRCodeView.Delegate {


    private var isOpenLight: Boolean = false
    //前置摄像头
    private var frontChange = false

    override fun setStatusBar() {
        StatusBarUtils.setStatusTransparent(this)
    }

    /**
     * 初始化
     */
    override fun init() {
        setContentView(R.layout.activity_qr_scan)
        mZXingView.setDelegate(this);
        rlBtnReturnQr.setOnClickListener { finish() }
        ivBtnLight.setOnClickListener {
            if (isOpenLight) {
                mZXingView.closeFlashlight(); // 关闭闪光灯
                isOpenLight = false
            } else {
                mZXingView.openFlashlight(); // 打开闪光灯
                isOpenLight = true
            }
        }
        ivBtnChange.setOnClickListener {
            onStop()
            if (frontChange) {
                mZXingView.startCamera(Camera.CameraInfo.CAMERA_FACING_BACK);
                frontChange = false
            } else {
                mZXingView.startCamera(Camera.CameraInfo.CAMERA_FACING_FRONT)
                CameraUtil.setCameraDisplayOrientation(
                    this,
                    Camera.CameraInfo.CAMERA_FACING_FRONT,
                    mZXingView.camera
                )
                frontChange = true
            }
            mZXingView.startSpotAndShowRect();
        }
    }

    /**
     * 开始扫描
     */
    override fun onStart() {
        super.onStart()


        BarcodeType.ALL
        mZXingView.startCamera(); // 打开后置摄像头开始预览，但是并未开始识别
//        mZXingView.startCamera(Camera.CameraInfo.CAMERA_FACING_FRONT); // 打开前置摄像头开始预览，但是并未开始识别

        mZXingView.startSpotAndShowRect(); // 显示扫描框，并开始识别
    }

    /**
     * 停止扫描
     */
    override fun onStop() {
        mZXingView.stopCamera(); // 关闭摄像头预览，并且隐藏扫描框
        super.onStop()
    }

    override fun onDestroy() {
        mZXingView.onDestroy() // 销毁二维码扫描控件
        super.onDestroy()
    }

    /**
     * 震动
     */
    private fun vibrate() {
        val vibrator = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
        vibrator.vibrate(200)
    }

    override fun onScanQRCodeSuccess(result: String) {
        Log.e("qrqr", "result:$result")
        //这里判断 是否是设备 等二维码
        if (!TextUtils.isEmpty(result)) {
//            if (result.startsWith(CompanionType.STRING_QR_MAC) || result.startsWith(CompanionType.STRING_QR_PACK) || result.startsWith(
//                    CompanionType.STRING_QR_BOM
//                ) || result.startsWith(CompanionType.STRING_QR_ORDER)
//            ) {
                //设备
                vibrate()
                val intent = Intent().putExtra(CompanionType.INTENT_EXTRA_QRCODE, result);
                setResult(Activity.RESULT_OK, intent)
                finish()
//            } else {
//                mZXingView.startSpot() // 开始识别
//            }
        } else {
            mZXingView.startSpot() // 开始识别
        }


    }

    override fun onCameraAmbientBrightnessChanged(isDark: Boolean) {
        //黑暗环境下
        if (isDark) {
            ivBtnLight.visibility = View.VISIBLE
        } else {
            if (isOpenLight) {
                ivBtnLight.visibility = View.VISIBLE
            } else {
                ivBtnLight.visibility = View.GONE
            }

        }
    }

    override fun onScanQRCodeOpenCameraError() {
        showToast(getString(R.string.text_open_camera_fail))
    }
}
